const ResponseModel = require('../models/response-model')
const jwtToken = require('../utils/jwt-token')


const whilteList = [
    '/api/auth/doAuth'
]

async function verifyToken(token) {
    if (token && token !== 'undefined' && await jwtToken.verifyToken(token)) {
        return true
    }
    return false
}

module.exports = function () {
    return async function ( ctx, next ) {
        const url = ctx.request.url
        const token = ctx.request.header['tm-token']
        if (!whilteList.includes(url) && !await verifyToken(token)) {
            ctx.response.body = ResponseModel.model(ResponseModel.CODE_NO_AUTH, '登录已过期')
        } else {
            await next()
        }
    }
}